package app.server.project.main.service.db.dao.user;

import app.server.project.core.constants.Def;
import app.server.project.main.service.db.entity.user.BlockTransactionEntity;
import app.server.project.main.service.db.mapper.user.BlockTransactionMapper;
import app.server.project.main.service.service.upay.UPayType;
import app.server.project.main.service.service.upay.tron.constants.TxStatus;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class BlockTransactionDao extends ServiceImpl<BlockTransactionMapper, BlockTransactionEntity> {


    public BlockTransactionEntity getBlockTransactionBy(int blockType, String transactionId) {
        var data = this.getOne(Wrappers.lambdaQuery(BlockTransactionEntity.class)
                .eq(BlockTransactionEntity::getBlockType, blockType)
                .eq(BlockTransactionEntity::getTransactionId, transactionId));
        return data;
    }


    public List<BlockTransactionEntity> getAllTxByUserId(long userId, int blockType) {
        return this.list(Wrappers.lambdaQuery(BlockTransactionEntity.class)
                .eq(BlockTransactionEntity::getUserId, userId)
                .eq(BlockTransactionEntity::getBlockType, blockType)
                .eq(BlockTransactionEntity::getTxFlag, TxStatus.Init)
                .eq(BlockTransactionEntity::getAddFlag, Def.FALSE)
        );
    }

    public void updateTxFlag(String txId) {
        this.update(Wrappers.lambdaUpdate(BlockTransactionEntity.class)
                .set(BlockTransactionEntity::getTxFlag, TxStatus.Success)
                .eq(BlockTransactionEntity::getTransferTxId, txId)
        );
    }

    public List<String> getAllUnUpdate() {
        return this.list(Wrappers.lambdaQuery(BlockTransactionEntity.class)
                        .eq(BlockTransactionEntity::getTxFlag, TxStatus.Fail)
                        .eq(BlockTransactionEntity::getBlockType, UPayType.Tron))
                .stream()
                .map(BlockTransactionEntity::getTransferTxId).toList();
    }


}
